<template>
  <div class="projects">
    <div class="projects-header">
      <h1>项目展示</h1>
      <p class="subtitle">精选作品集</p>
    </div>

    <div class="projects-filter">
      <button 
        v-for="category in categories" 
        :key="category"
        :class="['filter-btn', { active: currentFilter === category }]"
        @click="currentFilter = category"
      >
        {{ category }}
      </button>
    </div>

    <div class="projects-grid">
      <div 
        v-for="project in filteredProjects" 
        :key="project.id" 
        class="project-card"
      >
        <div class="project-image">
          <img :src="project.image" :alt="project.title">
          <div class="project-links">
            <a :href="project.demo" target="_blank" class="project-link">
              <i class="link-icon">🔗</i>
              演示
            </a>
            <a :href="project.github" target="_blank" class="project-link">
              <i class="link-icon">📦</i>
              源码
            </a>
          </div>
        </div>
        <div class="project-info">
          <h3>{{ project.title }}</h3>
          <p class="project-desc">{{ project.description }}</p>
          <div class="project-tech">
            <span 
              v-for="tech in project.technologies" 
              :key="tech" 
              class="tech-tag"
            >
              {{ tech }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useConfig } from '../utils/config'
import { siteConfig } from '../config/site'

// 获取项目配置
const projects = ref(siteConfig.projects)

// 调试输出
onMounted(() => {
  console.log('Projects data:', projects.value)
})

// 获取所有项目分类
const categories = computed(() => {
  if (!projects.value) return ['全部']
  const cats = ['全部', ...new Set(projects.value.map(p => p.category))]
  return cats
})

const currentFilter = ref('全部')

// 根据分类筛选项目
const filteredProjects = computed(() => {
  if (!projects.value) return []
  if (currentFilter.value === '全部') {
    return projects.value
  }
  return projects.value.filter(p => p.category === currentFilter.value)
})
</script>

<style lang="scss" scoped>
.projects {
  padding: 80px 20px;
  max-width: 1200px;
  margin: 0 auto;

  .projects-header {
    text-align: center;
    margin-bottom: 4rem;

    h1 {
      font-size: 3rem;
      color: var(--primary-color);
      margin-bottom: 1rem;
    }

    .subtitle {
      font-size: 1.5rem;
      color: var(--text-color);
      opacity: 0.8;
    }
  }
}

.projects-filter {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 4rem;
  flex-wrap: wrap;

  .filter-btn {
    padding: 0.8rem 1.5rem;
    border: none;
    border-radius: 25px;
    background: var(--background-color);
    color: var(--text-color);
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

    &:hover {
      transform: translateY(-2px);
    }

    &.active {
      background: var(--primary-color);
      color: white;
    }
  }
}

.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 2rem;
  padding: 1rem;

  .project-card {
    background: var(--background-color);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;

    &:hover {
      transform: translateY(-10px);

      .project-links {
        opacity: 1;
      }
    }

    .project-image {
      position: relative;
      padding-top: 60%;
      overflow: hidden;

      img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .project-links {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 2rem;
        opacity: 0;
        transition: opacity 0.3s ease;

        .project-link {
          padding: 0.8rem 1.5rem;
          background: var(--primary-color);
          color: white;
          text-decoration: none;
          border-radius: 25px;
          display: flex;
          align-items: center;
          gap: 0.5rem;
          font-size: 1.1rem;
          transition: transform 0.3s ease;

          &:hover {
            transform: scale(1.1);
          }

          .link-icon {
            font-size: 1.2rem;
          }
        }
      }
    }

    .project-info {
      padding: 2rem;

      h3 {
        font-size: 1.6rem;
        color: var(--text-color);
        margin-bottom: 1rem;
      }

      .project-desc {
        font-size: 1.1rem;
        color: var(--text-color);
        opacity: 0.8;
        margin-bottom: 1.5rem;
        line-height: 1.6;
      }

      .project-tech {
        display: flex;
        flex-wrap: wrap;
        gap: 0.8rem;

        .tech-tag {
          padding: 0.4rem 1rem;
          background: rgba(var(--primary-color-rgb), 0.1);
          color: var(--primary-color);
          border-radius: 15px;
          font-size: 0.9rem;
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .projects {
    padding: 60px 1rem;

    .projects-header {
      h1 {
        font-size: 2.5rem;
      }

      .subtitle {
        font-size: 1.2rem;
      }
    }
  }

  .projects-filter {
    gap: 0.5rem;
    margin-bottom: 2rem;

    .filter-btn {
      padding: 0.6rem 1rem;
      font-size: 1rem;
    }
  }

  .projects-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
    padding: 0;
  }
}
</style> 